package cz.najman.emdroid.content;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentValues;
import cz.najman.emdroid.listener.OnResponseDataParsedListener;

/**
 * Class that is used as ContentHandler in the parser
 * @author  Lukas Najman
 */
public class ContentHandler extends DefaultHandler{

	// Listener that is fire back when the data are successfully parsed
	/**
	 */
	protected OnResponseDataParsedListener mListener;
	// String builder used for reading the data from response stream
	protected final StringBuilder mBulider;
	// ContentValues serving as a container for the data
	protected ContentValues mValues;
	// ID representing the request
	protected Long mRequestId;
	// SipmleDateFormat used to format dates
	protected SimpleDateFormat mSdf;	
	
	public ContentHandler() {
		mBulider = new StringBuilder();
		mSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
	}
	
	@Override
	public void setDocumentLocator(Locator locator) {
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}
	
	/**
	 * Sets the listener that is fired back when the data were successfully parsed.
	 * The data are bundled to the listener as ContentValues
	 * @param listener
	 */
	public void setListener(OnResponseDataParsedListener listener){
		mListener = listener;
	}
	
	/**
	 * Sets the request ID of the request
	 * @param requestId
	 */
	public void setRequestId(Long requestId){
		mRequestId = requestId;
	}
	
	/** 
	 * @return Request ID
	 */
	public Long getRequestId(){
		return mRequestId;
	}
	
	protected Long getTimeFromString(String timeString){
		try {
			return mSdf.parse(mBulider.toString()).getTime();
		} catch (ParseException e) {			
			
			return 0l;
		}
	}

}
